/*
 * Copyright (c) 2015. 石头哥哥
 */

package com.comom.manager.kit.security;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Properties;

/**
 * @author 石头哥哥
 *         </P>
 *         Date:   2015/7/30
 *         </P>
 *         Time:   11:36
 *         </P>
 *         Package: server-java-chongmeng
 *         </P>
 *         <p/>
 *         注解：          用于处理加载properties 文件关键字段解密
 */
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {


    /**
     * 待解密key
     */
    private static final String url = "hikaricp.url";
    private static final String username = "hikaricp.username";
    private static final String passwrod = "hikaricp.password";
    private static final String poolname = "hikaricp.poolName";
    private static final String driver = "hikaricp.jdbc.driverClassName";


    /**
     * @param beanFactory
     * @param properties
     * @throws BeansException
     */
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties properties)
            throws BeansException {

        //解密 并替换掉之前的密文
        properties.setProperty(url, SecurityUtil.desDecrypt(properties.getProperty(url)));
        properties.setProperty(username, SecurityUtil.desDecrypt(properties.getProperty(username)));
        properties.setProperty(passwrod, SecurityUtil.desDecrypt(properties.getProperty(passwrod)));
        properties.setProperty(poolname, SecurityUtil.desDecrypt(properties.getProperty(poolname)));
        properties.setProperty(driver, SecurityUtil.desDecrypt(properties.getProperty(driver)));

        super.processProperties(beanFactory, properties);
    }
}
